/*  Beamed (the only limit is your imagination) */
/*  Copyright (C) 2012  Michael F�rst */
/*  */
/*  This program is free software; you can redistribute it and/or modify it */
/*  under the terms of the GNU General Public License as published by the */
/*  Free Software Foundation; either version 3 of the License, or (at your */
/*  option) any later version. */
/*  */
/*  This program is distributed in the hope that it will be useful, but */
/*  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY */
/*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License */
/*  for more details. */
/*  */
/*  You should have received a copy of the GNU General Public License along */
/*  with this program; if not, see <http://www.gnu.org/licenses/>. */

function CObject(Type, Params, Effects, Begin, End) {
	var type = Type;
	var params = Params;
	var effects = Effects;
	var that = this;
	var begin = -1;
	var end = -1;
	var local_dx = 0;
	var local_dy = 0;
	var local_screen_radius = 2000;
	this.bufferable = false; // TODO change that as soon as buffer is enabled

	if (!(typeof (Begin) === 'undefined')) {
		begin = Begin;
	}
	if (!(typeof (End) === 'undefined')) {
		end = End;
	}
	if (effects != null || type == B_Obj_Video || type == B_Obj_Audio) {
		buffereable = false;
	}

	this.draw = function(ctx, step, zoom, dx, dy, screen_radius) {
		local_dx = dx;
		local_dy = dy;
		local_screen_radius = screen_radius;
		/* only render if it is supposed to be visible */
		if ((begin < step) && (end > step || end <= -1)) {
			/* render to context */
			switch (type) {
			case (B_Obj_Text):
				text(ctx, zoom);
				break;
			case (B_Obj_Box):
				box(ctx, zoom);
				break;
			case (B_Obj_Circle):
				circle(ctx, zoom);
				break;
			case (B_Obj_Line):
				line(ctx, zoom);
				break;
			case (B_Obj_Video):
				video(ctx, zoom, step);
				break;
			case (B_Obj_Image):
				image(ctx, zoom);
				break;
			case (B_Obj_Audio):
				audio(ctx, zoom, step);
				break;
			default:
				custom(type, ctx, zoom);
			}
		}
	};
	this.custom = function(type, ctx, zoom) {
		/* just a dummy */
	};
	this.applyEffects = function(steppercent, step) {
		if (effects == null) {
			return;
		}
		for ( var i = 0; i < effects.length; i++) {
			effects[i].apply(that, steppercent, step);
		}
	};

	/* don't draw things that are not visible to improve the performance */
	function clipping(x, y, size, scalar) {
		// clipping bugged therefore disabled
		return true;
		if (lengthOf(local_dx / scalar + x, local_dy / scalar + y) < size
				+ (local_screen_radius / scalar)) {
			return true;
		}
		return false;
	}
	/* helper to calc length of a vect */
	function lengthOf(a, b) {
		return Math.sqrt((a * a) + (b * b));
	}

	var canv = null;
	var canv_scalar = 0;

	/* using scalar instead of zoom, because I did just copy from core 1.0 */
	function text(ctx, scalar) {
		/* this is not nice, but it improves performance drastically */
		if (clipping(params.x, params.y, params.size / 3 * params.str.length,
				scalar) == true
				&& (params.size * scalar) > 1 / 2) {

			if ((canv == null || canv_scalar / scalar < 0.75
					|| canv_scalar / scalar > 1.5)&&(scalar*params.size < 500)&&(scalar*params.size > 8)) {
				canv = document.createElement('canvas');
				canv.width = params.str.length * params.size / 1.5 * scalar;
				canv.height = params.size * scalar*1.5;
				if (canv.width < 1) {
					canv.width = 1;
				}
				if (canv.height < 1) {
					canv.height = 1;
				}
				var canv_ctx = canv.getContext('2d');
				canv_ctx.font = (params.size * scalar) + "px 'Helvetica'";
				canv_ctx.textAlign = 'center';
				canv_ctx.fillText(params.str, canv.width/2, canv.height/3*2);
				canv_scalar = scalar;
			} else if (canv == null && scalar*params.size >= 500) {
				canv = document.createElement('canvas');
				canv.width = params.str.length * 500 / 1.5;
				canv.height =  500*1.5;
				if (canv.width < 1) {
					canv.width = 1;
				}
				if (canv.height < 1) {
					canv.height = 1;
				}
				var canv_ctx = canv.getContext('2d');
				canv_ctx.font = (500) + "px 'Helvetica'";
				canv_ctx.textAlign = 'center';
				canv_ctx.fillText(params.str, canv.width/2, canv.height/3*2);
				canv_scalar = 500/params.size;
			} else if (canv == null && scalar*params.size <= 8) {
				canv = document.createElement('canvas');
				canv.width = params.str.length * 8 / 1.5;
				canv.height =  8*1.5;
				if (canv.width < 1) {
					canv.width = 1;
				}
				if (canv.height < 1) {
					canv.height = 1;
				}
				var canv_ctx = canv.getContext('2d');
				canv_ctx.font = (8) + "px 'Helvetica'";
				canv_ctx.textAlign = 'center';
				canv_ctx.fillText(params.str, canv.width/2, canv.height/3*2);
				canv_scalar = 8/params.size;
			}

			ctx.translate(params.x * scalar, (params.y) * scalar);
			ctx.rotate((params.angle) * Math.PI / 180);
			ctx.translate(-params.x * scalar, -(params.y) * scalar);
			/*
			 * ctx.font = (params.size * scalar) + "px 'Helvetica'";
			 * ctx.textAlign = 'center'; ctx.fillText(params.str, (params.x) *
			 * scalar, (params.y - params.size / 3) * scalar);
			 */
			var temp_scalar = scalar / canv_scalar;
			if (canv != null) {
				ctx.drawImage(canv,
						params.x*scalar -((canv.width /2) * temp_scalar),
						params.y*scalar -((canv.height/2) * temp_scalar),
						canv.width * temp_scalar, canv.height * temp_scalar);
			}
			ctx.translate(params.x * scalar, (params.y) * scalar);
			ctx.rotate((-params.angle) * Math.PI / 180);
			ctx.translate(-params.x * scalar, -(params.y) * scalar);
		}
	}
	function box(ctx, scalar) {
		if (clipping(params.x, params.y,
				lengthOf(params.width, params.height) / 2, scalar) == true) {
			box_y = params.y - (params.height / 2);
			box_x = params.x - (params.width / 2);
			ctx.translate(params.x * scalar, params.y * scalar);
			ctx.rotate((params.angle) * Math.PI / 180);
			ctx.translate(-params.x * scalar, -params.y * scalar);
			var xpos = box_x * scalar;
			var ypos = box_y * scalar;
			if (params.round) {
				ctx.strokeRect(xpos, ypos, params.width * scalar, params.height
						* scalar);
			} else {
				if (params.filled == true) {
					ctx.fillRect(xpos, ypos, params.width * scalar,
							params.height * scalar);
				} else {
					ctx.strokeRect(xpos, ypos, params.width * scalar,
							params.height * scalar);
				}
			}
			ctx.translate(params.x * scalar, params.y * scalar);
			ctx.rotate(-(params.angle) * Math.PI / 180);
			ctx.translate(-params.x * scalar, -params.y * scalar);
		}
	}
	function circle(ctx, scalar) {
		if (clipping(params.x, params.y, params.radius, scalar) == true) {
			ctx.moveTo(params.x, params.y);
			ctx.beginPath();
			ctx.arc(params.x * scalar, params.y * scalar, params.radius
					* scalar, 0, Math.PI * 2, true);
			if (params.filled == true) {
				ctx.fill();
			} else {
				ctx.stroke();
			}
		}
	}
	function line(ctx, scalar) {
		if (clipping((params.x + params.x2) / 2, (params.y + params.y2) / 2,
				lengthOf(params.x2 - params.x, params.y2 - params.y) / 2,
				scalar) == true) {
			ctx.beginPath();
			ctx.moveTo(params.x * scalar, params.y * scalar);
			ctx.lineTo(params.x2 * scalar, params.y2 * scalar);
			ctx.stroke();
		}
	}
	function video(ctx, zoom, step) {
		if (step >= params.begin && step <= params.end) {
			if (params.running != true) {
				params.obj.play();
				params.running = true;
			}
			if (clipping(params.x, params.y, lengthOf(params.width,
					params.height) / 2, scalar) == true) {
				var vid_y = params.y - (params.height / 2);
				var vid_x = params.x - (params.width / 2);
				ctx.translate((vid_x + params.width / 2) * scalar,
						(vid_y + params.height / 2) * scalar);
				ctx.rotate((params.angle) * Math.PI / 180);
				ctx.translate(-(vid_x + params.width / 2) * scalar,
						-(vid_y + params.height / 2) * scalar);
				var xpos = vid_x * scalar;
				var ypos = vid_y * scalar;
				/* draw the scaled image */
				ctx.drawImage(params.obj, xpos, ypos, params.width * scalar,
						params.height * scalar);
				ctx.translate((vid_x + params.width / 2) * scalar,
						(vid_y + params.height / 2) * scalar);
				ctx.rotate((-params.angle) * Math.PI / 180);
				ctx.translate(-(vid_x + params.width / 2) * scalar,
						-(vid_y + params.height / 2) * scalar);
			}
		} else {
			if (clipping(params.x, params.y, lengthOf(params.width,
					params.height) / 2, scalar) == true) {
				var vid_y = params.y - (params.height / 2);
				var vid_x = params.x - (params.width / 2);
				ctx.translate((vid_x + params.width / 2) * scalar,
						(vid_y + params.height / 2) * scalar);
				ctx.rotate((params.angle) * Math.PI / 180);
				ctx.translate(-(vid_x + params.width / 2) * scalar,
						-(vid_y + params.height / 2) * scalar);
				var xpos = vid_x * scalar;
				var ypos = vid_y * scalar;
				/* draw the scaled image */
				ctx.drawImage(params.obj, xpos, ypos, params.width * scalar,
						params.height * scalar);
				var temp = ctx.fillStyle;
				ctx.fillStyle = "rgba(0,0,0,0.5)";
				ctx.fillRect(xpos, ypos, params.width * scalar, params.height
						* scalar);
				ctx.fillStyle = temp;
				ctx.translate((vid_x + params.width / 2) * scalar,
						(vid_y + params.height / 2) * scalar);
				ctx.rotate((-params.angle) * Math.PI / 180);
				ctx.translate(-(vid_x + params.width / 2) * scalar,
						-(vid_y + params.height / 2) * scalar);
			}
			params.obj.pause();
			params.running = false;
		}
	}
	function image(ctx, zoom) {
		if (clipping(params.x, params.y,
				lengthOf(params.width, params.height) / 2, scalar) == true) {
			img_y = params.y - (params.height / 2);
			img_x = params.x - (params.width / 2);
			ctx.translate((img_x + params.width / 2) * scalar,
					(img_y + params.height / 2) * scalar);
			ctx.rotate((params.angle) * Math.PI / 180);
			ctx.translate(-(img_x + params.width / 2) * scalar,
					-(img_y + params.height / 2) * scalar);
			var xpos = img_x * scalar;
			var ypos = img_y * scalar;
			/* draw the scaled image */
			ctx.drawImage(params.obj, xpos, ypos, params.width * scalar,
					params.height * scalar);
			ctx.translate((img_x + params.width / 2) * scalar,
					(img_y + params.height / 2) * scalar);
			ctx.rotate((-params.angle) * Math.PI / 180);
			ctx.translate(-(img_x + params.width / 2) * scalar,
					-(img_y + params.height / 2) * scalar);
		}
	}
	function audio(ctx, zoom, step) {
		if (step >= params.begin && step <= params.end) {
			if (params.data_audio_running != true) {
				params.obj.play();
				params.running = true;
			}
		} else {
			params.obj.pause();
			params.running = false;
		}
	}
}
